home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pctchnqs / 1991 / number2 / enumfunc.pas < prev    next >
Pascal/Delphi Source File  |  1990-12-13  |  2KB  |  64 lines

  1.  
  2. PROGRAM EnumFunc;
  3. {----------------------------------------------------------------
  4.   This program demonstates the use of a declared enumerated data
  5.   type in a "function-like" call to retrieve the enumeration
  6.   state corresponding to a known ordinal (i.e., it performs the
  7.   inverse of the Ord() function).
  8.  
  9.   For PC TECHNIQUES by George W. Seaton.
  10. ----------------------------------------------------------------}
  11. USES Crt;
  12. {----------------------------------------------------------------
  13.   Define an enumerated data type
  14. ----------------------------------------------------------------}
  15. TYPE
  16.   Suit = (Spades,Hearts,Clubs,Diamonds);
  17. VAR
  18. {---------------------------------------------------------------
  19.   MySuit is an instance of type Suit
  20. ---------------------------------------------------------------}
  21.   MySuit : Suit;
  22.  
  23.   N      : Integer;
  24.   Ch     : Char;
  25.   Done   : Boolean;
  26. {==============================================================}
  27. BEGIN
  28.   Done := FALSE;
  29.   ClrScr;
  30. {---------------------------------------------------------------
  31.   Ask the user for a number corresponding to one of the Suit
  32.   states.  As illustrated, it does NOT have to be the exact
  33.   ordinal for the state.
  34. ---------------------------------------------------------------}
  35.   WHILE (NOT Done) DO BEGIN
  36.     WriteLn('Enter a suit number (1-4) or zero to quit.');
  37.     WriteLn('(1 = Spades, 2 = Hearts, 3 = Clubs, 4 = Diamonds)');
  38.     REPEAT
  39.       Ch := ReadKey;
  40.     UNTIL (Ch IN ['0'..'4']);
  41.     WriteLn(Ch);
  42.     N := Ord(Ch) - $30;
  43.     IF (N = 0) THEN Done := TRUE
  44.     ELSE BEGIN
  45. {---------------------------------------------------------------
  46.   Set the Enumerated VARIABLE (MySuit) to the proper state by
  47.   creating the ordinal value (N-1) and using it as the parameter
  48.   for the type Suit "function call."
  49. ---------------------------------------------------------------}
  50.       MySuit := Suit(N-1);                           (* NOTE! *)
  51. {---------------------------------------------------------------
  52.   Act on the current state of MySuit.
  53. ---------------------------------------------------------------}
  54.       Write('You selected ');
  55.       CASE MySuit OF
  56.         Spades   : WriteLn('SPADES!');
  57.         Hearts   : WriteLn('HEARTS!');
  58.         Clubs    : WriteLn('CLUBS!');
  59.         Diamonds : WriteLn('DIAMONDS!');
  60.       END;
  61.     END;
  62.   END;
  63. END.
  64.